package com.ego.item.web;

import com.ego.common.pojo.PageResult;
import com.ego.item.bo.SpuBo;
import com.ego.item.pojo.Sku;
import com.ego.item.pojo.SpuDetail;
import com.ego.item.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 *  @author: xingming
 *  @Date: 2020/2/24 11:23
 *  @Description:
 */
@RestController
@RequestMapping("/goods")
public class GoodsController {
    @Autowired
    private GoodsService goodsService;
    @GetMapping("/spu/page")
    public ResponseEntity<PageResult<SpuBo>> page(
            @RequestParam("key")String key,
            @RequestParam("saleable")Boolean saleable,
            @RequestParam(value = "page",defaultValue = "1")Integer page,
            @RequestParam(value = "rows",defaultValue = "10")Integer rows
            )
    {
        PageResult<SpuBo> result=goodsService.page(key,saleable,page,rows);
        if(result==null){
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(result);
    }
    @PostMapping
    public ResponseEntity<Void> save(@RequestBody SpuBo spuBo){
        goodsService.save(spuBo);
        return ResponseEntity.ok().build();
    }
    @GetMapping("spu/detail/{spuId}")
    public ResponseEntity<SpuDetail> querySpuDetailBySpuId(@PathVariable("spuId")Long spuId){
        SpuDetail result= goodsService.findSpuDetailBySpuId(spuId);
        if(result==null){
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(result);
    }

    @GetMapping("/sku/list/{spuId}")
    public ResponseEntity<List<Sku>> querySkuBySpuId(@PathVariable("spuId")Long spuId){
        List<Sku> result = goodsService.findSkuBySpuId(spuId);
        if(result==null){
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(result);
    }
    @PutMapping
    public ResponseEntity<Void> update(@RequestBody SpuBo spuBo){
        goodsService.update(spuBo);
        return ResponseEntity.ok().build();
    }
}
